
package produce

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/produce"
    produceReq "github.com/flipped-aurora/gin-vue-admin/server/model/produce/request"
)

type GroupService struct {}
// CreateGroup 创建设备分组记录
// Author [yourname](https://github.com/yourname)
func (groupService *GroupService) CreateGroup(ctx context.Context, group *produce.Group) (err error) {
	err = global.GVA_DB.Create(group).Error
	return err
}

// DeleteGroup 删除设备分组记录
// Author [yourname](https://github.com/yourname)
func (groupService *GroupService)DeleteGroup(ctx context.Context, id string) (err error) {
	err = global.GVA_DB.Delete(&produce.Group{},"id = ?",id).Error
	return err
}

// DeleteGroupByIds 批量删除设备分组记录
// Author [yourname](https://github.com/yourname)
func (groupService *GroupService)DeleteGroupByIds(ctx context.Context, ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]produce.Group{},"id in ?",ids).Error
	return err
}

// UpdateGroup 更新设备分组记录
// Author [yourname](https://github.com/yourname)
func (groupService *GroupService)UpdateGroup(ctx context.Context, group produce.Group) (err error) {
	err = global.GVA_DB.Model(&produce.Group{}).Where("id = ?",group.Id).Updates(&group).Error
	return err
}

// GetGroup 根据id获取设备分组记录
// Author [yourname](https://github.com/yourname)
func (groupService *GroupService)GetGroup(ctx context.Context, id string) (group produce.Group, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&group).Error
	return
}
// GetGroupInfoList 分页获取设备分组记录
// Author [yourname](https://github.com/yourname)
func (groupService *GroupService)GetGroupInfoList(ctx context.Context, info produceReq.GroupSearch) (list []produce.Group, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&produce.Group{})
    var groups []produce.Group
    // 如果有条件搜索 下方会自动创建搜索语句
    
    if info.GroupName != nil && *info.GroupName != "" {
        db = db.Where("group_name LIKE ?", "%"+ *info.GroupName+"%")
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&groups).Error
	return  groups, total, err
}
func (groupService *GroupService)GetGroupPublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
